﻿using RabbitMQ.Servers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using helper;
using Newtonsoft.Json;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using RabbitMQWeb.DB;
using WYYserver_http_api;

namespace RabbitMQ.WindowsService
{

    public class Hander
    {
        private static string emailAcount = "1484821849@qq.com";//发件人地址
        private static string emailPassword = "";//发件人邮箱授权码
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="reciver">收件人</param>
        /// <param name="content">收件人内容</param>
        public void SendEmail(string reciver, string content)
        {
            try
            {
                //实例化一个发送邮件类。
                MailMessage mailMessage = new MailMessage();
                //发件人邮箱地址，方法重载不同，可以根据需求自行选择。
                mailMessage.From = new MailAddress(emailAcount);
                //收件人邮箱地址。
                mailMessage.To.Add(new MailAddress(reciver));
                //邮件标题。
                mailMessage.Subject = "木槿科技";
                //邮件内容。
                mailMessage.Body = content;

                //实例化一个SmtpClient类。
                SmtpClient client = new SmtpClient();
                //在这里我使用的是qq邮箱，所以是smtp.qq.com，如果你使用的是126邮箱，那么就是smtp.126.com。
                client.Host = "smtp.qq.com";
                //使用安全加密连接。
                client.EnableSsl = true;
                //不和请求一块发送。
                client.UseDefaultCredentials = false;
                //验证发件人身份(发件人的邮箱，邮箱里的生成授权码);
                client.Credentials = new NetworkCredential(emailAcount, emailPassword);
                //发送
                client.Send(mailMessage);
            }
            catch (Exception e)
            {
                LoggerHelper.Log(e.Message);
            }
        }

        public void HanderStart()
        {
            using (var connection = RabbitMQHelper._connectionFactory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    bool durable = true;
                    channel.QueueDeclare("task_queue", durable, false, false, null);
                    channel.BasicQos(0, 1, false);

                    var consumer = new QueueingBasicConsumer(channel);
                    channel.BasicConsume("task_queue", false, consumer);

                    while (true)
                    {
                        var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();

                        var body = ea.Body;
                        var message = Encoding.UTF8.GetString(body);
                        var obj = JsonConvert.DeserializeObject<RabbitMQ_UserInfo>(message);
                        //处理业务
                        LoggerHelper.Log(obj.Name);
                        //1,发送邮件
                        SendEmail(obj.Email, $"尊敬的用户{obj.Name},恭喜你注册成为我们网站的用户,欢迎您加入!");
                        //2,发送短信
                        Mesage.SendMessage(obj.Tel);
                        LoggerHelper.Log("短信发送成功");
                        channel.BasicAck(ea.DeliveryTag, false);
                    }
                }
            }
        }

    }
}
